// CareerEJB.java
package career;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import descriptor.CareerDescriptor;

import user.User;
import user.UserHome;

/**
 * CareerBean
 * 
 * @author Mamadou TOURE
 * 
 * The CareerBean class implements all the methods that a remote client expect to find
 * when it calls the interface (Career).
 * 
 * Note : 	-We use @SuppressWarnings("unchecked") to map the persistent schema of the database
 * 			 and to avoid the warning about the collection element type.
 */
public abstract class CareerBean implements EntityBean 
{
	InitialContext initialContext;
	UserHome userHome;

	// <ATTRIBUTES>
	
	// simple attributes
	/** 
	 * getId
	 * 
	 * The getId method returns a integer which indicates user identification number of a user.
	 * 
	 * @return		The user identification number of a user.
	 */
	public abstract int getId();
	
	/** 
	 * setId
	 * 
	 * The setId method sets the user identification number of a user.
	 * 
	 * @param id		The new user identification number that we want to add.
	 */
	public abstract void setId(int id);
	
	/** 
	 * getLogin
	 * 
	 * The getLogin method returns a string which indicates the login name that user have to provide to log into the application.
	 * 
	 * @return		The login name to provide by the user.
	 */
	public String getLogin()
	{
		return getUser().getLogin();
	}
	
	/** 
	 * setLogin
	 * 
	 * The setLogin method sets the login name of a user.
	 * 
	 * @param login		The new login name that we want to add.
	 */
	public void setLogin(String login)
	{
		getUser().setLogin(login);
	}
	
	/** 
	 * getPassword
	 * 
	 * The getPassword method returns a string which indicates the password that user have to provide to log into the application.
	 * 
	 * @return		The password to provide by the user.
	 */
	public String getPassword()
	{
		return getUser().getPasswd();
	}
	
	/** 
	 * setPassword
	 * 
	 * The setPassword method sets the password of a user.
	 * 
	 * @param password		The new password that we want to add.
	 */
	public void setPassword(String password)
	{
		getUser().setPasswd(password);
	}
	
	/** 
	 * getFirstname
	 * 
	 * The getFirstname method returns a string which indicates the firstname of a user.
	 * 
	 * @return		The user's firstname.
	 */
	public String getFirstname()
	{
		return getUser().getFirstName();
	}
	
	/** 
	 * setFirstname
	 * 
	 * The setFirstname method sets the firstname of a user.
	 * 
	 * @param firstname		The firstname that we want to add.
	 */
	public void setFirstname(String firstname)
	{
		getUser().setFirstName(firstname);
	}
	
	/** 
	 * getSurname
	 * 
	 * The getSurname method returns a string which indicates the surname of a user.
	 * 
	 * @return		The user's surname.
	 */
	public String getSurname()
	{
		return getUser().getName();
	}
	
	/** 
	 * setSurname
	 * 
	 * The setSurname method sets the surname of a user.
	 * 
	 * @param surname		The surname that we want to add.
	 */
	public void setSurname(String surname)
	{
		getUser().setName(surname);
	}
	
	/** 
	 * getEmail
	 * 
	 * The getEmail method returns a string which indicates the email address of a user.
	 * 
	 * @return		The email address of a user.
	 */
	public String getEmail()
	{
		return getUser().getMail();
	}
	
	/** 
	 * setEmail
	 * 
	 * The setEmail method sets the email address of a user.
	 * 
	 * @param surname		The new email address that we want to add.
	 */
	public void setEmail(String email)
	{
		getUser().setMail(email);
	}
	
	// Bean attributes
	// relations 1-1
	/** 
	 * getUser
	 * 
	 * The getUser abstract method returns an object of type User which provides the user details,
	 * (eg. surname, firstname, login, password,......).
	 * 
	 * @return		A user's object.
	 */
	public abstract User getUser();
	
	/** 
	 * setUser
	 * 
	 * The setUser abstract method sets an object of type User.
	 * 
	 * @param user		A new user's object..
	 */
	public abstract void setUser(User user);
    
	/** 
	 * ejbCreate
	 * 
	 * The ejbCreate method defines the operations to follow to create an EJB.
	 * This method refers to the method create in the remote interface.
	 * 
	 * @param firstName			User's firstname.
	 * @param name				User's surname.
	 * @param login				User's login name.
	 * @param passwd			User's password.
	 * @param mail				User's email address.
	 * 
	 * @return 					Return's values is always null.
	 */
    public String ejbCreate(String firstName,String name, String login,String passwd,String email) throws CreateException
    {
    	try
    	{
			initialContext = new InitialContext();
			userHome = (UserHome)initialContext.lookup("java:comp/env/ejb/User");
		}
    	catch (NamingException e) 
    	{
			e.printStackTrace();
		}
		User u = null;
		u = userHome.create(firstName, name, login, passwd, email);
		u.setType(User.CAREER);
		setId(u.getId());
		return null;
    }
    
    /** 
	 * ejbPostCreate
	 * 
	 * The ejbPostCreate method defines the operations to follow after creating an EJB.
	 * 
	 * @param firstName			User's firstname.
	 * @param name				User's surname.
	 * @param login				User's login name.
	 * @param passwd			User's password.
	 * @param mail				User's email address.
	 * 
	 */
    public void ejbPostCreate(String firstName,String name, String login,String passwd,String email) 
    {
    	User u = null;
		try 
		{
			u = userHome.findByPrimaryKey(getId());
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		setUser(u);
    }
    
    /** 
	 * ejbRemove
	 * 
	 * The ejbRemove method defines the operations to follow to delete an EJB.
	 *
	 */
    public void ejbRemove() throws RemoveException 
    {
    	int id = getId();
		User u = null;
		try
		{
			u = userHome.findByPrimaryKey(id);
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		try 
		{
			u.remove();
		} 
		catch (RemoveException e) 
		{
			e.printStackTrace();
		}
    }
    
    
    /** 
	 * getDescriptor
	 * 
	 * The getDescriptor method provides a Java object instead of a bean object. Hence, the object is directly accessible from a servlet or a JSP. 
	 * 
	 * @param getId()					The ID of the student
	 * @param getFirstName()			The student's firstname
	 * @param getName()					The student's surname
	 * @param getLogin()				The student's login
	 * @param getPasswd()				The student's password
	 * @param getMail()					The student's email address
	 * @param User.CAREER				The student's integer type (=4)
	 * 
	 * @return 							A Career Descriptor's object.
	 */
	public CareerDescriptor getDescriptor()
	{
		return new CareerDescriptor(getId(),getFirstname(),getSurname(),getLogin(),getPassword(),getEmail(),User.CAREER);
	}
    
    public void ejbActivate() throws EJBException, RemoteException {}
	public void ejbPassivate() throws EJBException, RemoteException {}
	public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {}
	public void ejbLoad() throws EJBException, RemoteException {}
	public void ejbStore() throws EJBException, RemoteException {}
	public void setEntityContext(EntityContext arg0) throws EJBException, RemoteException {}
	public void unsetEntityContext() throws EJBException, RemoteException {}
}



